home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / sorting.swg / 0057_Sort a data file on disk.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-22  |  2.7 KB  |  97 lines

  1.  
  2. {$A+,B-,D-,E-,F-,G+,I-,K-,L-,N-,O-,P-,Q-,R-,S-,T-,V-,W-,X-,Y-}
  3.  
  4. {Sorts a data file on disk}
  5.  
  6. {*WARNING*: This program will create two .5Mb files for test
  7.  purposes. Reduce the constant "Max" to about 100 if you only
  8.  want a quick look at the program.}
  9.  
  10. PROGRAM DiskSort;
  11. USES  Dos;                          {For start/stop time}
  12. CONST Max=1000;                     {Number of records}
  13. TYPE  OneRecord=RECORD              {550 bytes}
  14.                  S:String[20];      {The sorted field}
  15.                  W:Word;            {Some other fields..}
  16.                  B:Byte;
  17.                  I:Integer;
  18.                  R:Real;
  19.                  P:Pointer;
  20.                  L:LongInt;
  21.                  X:String;
  22.                  Y:String;
  23.                 END;
  24.  
  25.       FileType=File OF OneRecord;
  26. {------------------------------------------------}
  27. {This routine creates the data file and writes
  28.  randomly generated records in it for testing.}
  29.  
  30. PROCEDURE CreateDataFile(FileName:String);
  31. VAR This:OneRecord; F:FileType; N,X,C:Integer;
  32. BEGIN
  33.  Assign(F,FileName); Rewrite(F);
  34.  FOR N:=1 TO Max DO
  35.   BEGIN
  36.    FillChar(This,SizeOf(This),#0);
  37.    FOR X:=1 TO 20 DO
  38.     BEGIN
  39.      REPEAT
  40.       C:=Random(100);
  41.      UNTIL C IN [65..90];
  42.      This.S:=This.S+Chr(C);
  43.     END;
  44.    Write(F,This);
  45.   END; Close(F);
  46. END;
  47. {------------------------------------------------}
  48. {This routine sorts the data file and puts the sorted
  49.  records in the temp file.}
  50.  
  51. PROCEDURE SortDataFile(FileName,TempName:String);
  52. VAR Old,This,Saved,Temp:OneRecord; F1,F2:FileType;
  53.     N1,N2,N3:LongInt; SavedStr:String[20];
  54.  {-----------------------------------------------}
  55.  PROCEDURE CheckIt;  {comparison routine}
  56.   BEGIN
  57.    IF This.S<SavedStr THEN
  58.     BEGIN
  59.      SavedStr:=This.S;
  60.      Temp:=This; This:=Saved; Saved:=Temp;
  61.      N3:=Pred(FilePos(F1));
  62.     END;
  63.   END;
  64.  {-----------------------------------------------}
  65. BEGIN
  66.  Assign(F1,FileName); Reset(F1);
  67.  Assign(F2,TempName); Rewrite(F2);
  68.  N1:=0; N2:=FileSize(F1); N3:=0;
  69.  REPEAT
  70.   Seek(F1,N1); Read(F1,Old);
  71.   SavedStr:=Old.S; This:=Old; Saved:=Old;
  72.   WHILE NOT EOF(F1) DO
  73.    BEGIN CheckIt; Read(F1,This); END;
  74.   CheckIt;
  75.   Seek(F1,N3); Write(F1,Old);
  76.   Seek(F2,FileSize(F2)); Write(F2,Saved);
  77.   Inc(N1);
  78.  UNTIL N1>=N2;
  79.  Close(F1); Close(F2);
  80. END;
  81. {------------------------------------------------}
  82. VAR S1,S2:String; H,M,S,U:Word;
  83. BEGIN
  84.  Randomize;
  85.  S1:='MIXED.DAT'; S2:='SORTED.DAT';
  86.  Writeln;
  87.  Writeln('Creating the data file ',S1);
  88.  CreateDataFile(S1);
  89.  Writeln;
  90.  Writeln('Now sorting it as ',S2);
  91.  GetTime(H,M,S,U);
  92.  Writeln('Start : ',H,':',M,':',S,'.',U);
  93.  SortDataFile(S1,S2);
  94.  GetTime(H,M,S,U);
  95.  Writeln('Stop  : ',H,':',M,':',S,'.',U);
  96. END.
  97.